<!doctype html>
<html>
  <head>
    <title>XMLHttpRequest: responseXML document properties</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="../html/dom/documents/resource-metadata-management/support/document-lastModified-utils.js"></script>
  </head>
  <body>
    <div id="log"></div>
    <script>
      var client = new XMLHttpRequest()
      client.open("GET", "resources/well-formed.xml", false)
      client.send(null)
      var responseURLObject = new URL('resources/well-formed.xml', location.href);
      var responseURL = responseURLObject.href
      var responseDomain = responseURLObject.hostname
      var expected = {
        domain:responseDomain,
        URL:responseURL,
        documentURI:responseURL,
        baseURI:responseURL,
        referrer:'',
        title:'',
        contentType:'application/xml',
        readyState:'complete',
        location:null,
        defaultView:null,
        body:null,
        doctype:null,
        all:HTMLAllCollection,
        cookie:''
      }

      for (var name in expected) {
        runTest(name, expected[name])
      }

      function runTest(name, value){
        test(function(){
          if (name == "all") {
            assert_equals(Object.getPrototypeOf(client.responseXML[name]), value.prototype)
          } else {
            assert_equals(client.responseXML[name], value)
          }
        }, name)
      }

      // Parse a "lastModified" value and convert it to a Date.
      // See https://html.spec.whatwg.org/multipage/dom.html#dom-document-lastmodified
      function parseLastModified(value) {
        const [undefined, month, day, year, hours, minutes, seconds] =
          /^(\d\d)\/(\d\d)\/(\d+) (\d\d):(\d\d):(\d\d)$/.exec(value);
        return new Date(year, month - 1, day, hours, minutes, seconds);
      }

      async_test(t => {
        const client = new XMLHttpRequest();
        client.open("GET", "resources/redirect.py?location=well-formed.xml");
        client.send();
        client.onload = t.step_func_done(() => {
          assert_equals(client.responseXML.URL, responseURL);
          assert_equals(client.responseXML.baseURI, responseURL);
        });
      }, "Test document URL properties after redirect");

      async_test(t => {
        const client = new XMLHttpRequest();
        client.open("GET", "resources/redirect.py?location=base.xml");
        client.send();
        client.onload = t.step_func_done(() => {
          const localResponseURL = new URL('resources/base.xml', location.href).href;
          assert_equals(client.responseXML.URL, localResponseURL);
          assert_equals(client.responseXML.baseURI, 'https://example.com/');
          client.responseXML.documentElement.remove();
          assert_equals(client.responseXML.baseURI, localResponseURL);
          const newBase = document.createElement("base"),
                newBaseURL = "https://elsewhere.example/";
          newBase.href = "https://elsewhere.example/";
          client.responseXML.appendChild(newBase);
          assert_equals(client.responseXML.baseURI, newBaseURL);
          newBase.remove();
          document.head.appendChild(newBase);
          assert_equals(client.responseXML.baseURI, localResponseURL);
          newBase.remove();
        });
      }, "Test document URL properties of document with <base> after redirect");

      test(function() {
        assert_document_lastmodified_string_approximately_now(client.responseXML.lastModified);
      }, 'lastModified set to time of response if no HTTP header provided')

      test(function() {
        var client2 = new XMLHttpRequest()
        client2.open("GET", "resources/last-modified.py", false)
        client2.send(null)
        assert_equals((new Date(client2.getResponseHeader('Last-Modified'))).getTime(), (parseLastModified(client2.responseXML.lastModified)).getTime())
      }, 'lastModified set to related HTTP header if provided')

      test(function() {
        client.responseXML.cookie = "thisshouldbeignored"
        assert_equals(client.responseXML.cookie, "")
      }, 'cookie (after setting it)')

      var objectProps = [
        "styleSheets",
        "implementation",
        "images",
        "forms",
        "links",
      ];

      for (let prop of objectProps) {
        test(function() {
          assert_equals(typeof(client.responseXML[prop]), "object")
        }, prop + " should be an object")
      }
    </script>
  </body>
</html>
